/*
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.samples.apps.topeka.activity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.samples.apps.topeka.base.R
import com.google.samples.apps.topeka.fragment.SignInFragment
import com.google.samples.apps.topeka.helper.ActivityLaunchHelper.Companion.EXTRA_EDIT
import com.google.samples.apps.topeka.helper.isLoggedIn
import com.google.samples.apps.topeka.helper.findFragmentById
import com.google.samples.apps.topeka.helper.replaceFragment
import android.util.Log
class SignInActivity : AppCompatActivity() {
	companion object{
		private const val TAG = "MAIN_ACT"
	}
	override fun onCreate(savedInstanceState: Bundle?) {
		val scg:String = savedInstanceState?.toString() ?: "<null>"
		Log.d(TAG,"1.onCreate($scg)")
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_sign_in)
		if (savedInstanceState == null) {
			replaceFragment(R.id.sign_in_container, SignInFragment.newInstance(isInEditMode || !isLoggedIn()))
		}
	}
	override fun onStart() {
		super.onStart()
		Log.d(TAG,"2.onStart()")
	}
	override fun onRestart() {
		super.onRestart()
		Log.d(TAG,"3.onReStart()")
	}
	override fun onResume() {
		super.onResume()
		Log.d(TAG,"4.onResume()")
	}
	override fun onStop() {
		super.onStop()
		Log.d(TAG,"5.onStop()")
		if (isLoggedIn()) finish()
	}
	override fun onPause() {
		super.onPause()
		Log.d(TAG, "6.onPause()")
	}
	override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
		super.onActivityResult(requestCode, resultCode, data)
		// Forwarding all results to SignInFragment for further handling.
		findFragmentById(R.id.sign_in_container)?.onActivityResult(requestCode, resultCode, data)
	}
	override fun onDestroy() {
		super.onDestroy()
		Log.d(TAG,"7.onDestroy()")
		if(!isLoggedIn()){
			kotlin.system.exitProcess(0)
		}
	}
	private val isInEditMode get() = intent.getBooleanExtra(EXTRA_EDIT, false)
}
